<template>
  <base-breadcrumb :title="title">
    <!-- 发起资料评审 -->
    <a-form-model
      ref="ruleForm"
      :model="form"
      :label-col="{ span: 6 }"
      :wrapper-col="{ span: 18 }"
      :rules="editModel != 0 ? rules : {}"
      id="base-breadcrumb"
      style="position: relative"
    >
      <a-spin :spinning="loading">
        <a-card style="margin-bottom: 50px">
          <!-- 基础信息 -->
          <CustomForm :formList="formList" :isEmit="false" :form="form" :editModel="editModel == 0 ? 'V' : ''" />
          <!-- 根据editModel加载不同组件：1 发起评审；2 资料扣分；3 成果发布 -->
          <component ref="table" :api="api" :form="form" :is="activeComponent" />
        </a-card>
      </a-spin>
      <!-- 页脚操作栏 -->
      <div class="btn_box tool-btns--fixed page-btn-right-top">
        <a-button v-if="editModel != 0" :loading="loading" type="primary" class="button_right" @click="handleSubmit">
          {{ editModel == 3 ? '发布成果' : '提交' }}
        </a-button>
        <a-button v-if="checkVisibleInDetail" type="primary" @click="handleActiveClose">关闭</a-button>
      </div>
    </a-form-model>
  </base-breadcrumb>
</template>

<script>
import BaseEditPage from '@/views/baseEditPage' // 编辑详情页基础配置
import { qcActivityAchievementReview } from '@/api/quality/quality' // api

export default {
  name: 'a' + Date.now(),
  extends: new BaseEditPage(),
  computed: {
    // 根据editModel动态加载不同组件：1 发起评审；2 资料扣分；3 成果发布
    activeComponent() {
      console.log(this.editModel, 'this.editModel')
      let componet = require(`./${
        ['ViewData', 'InitiateReview', 'DataDeduction', 'ReleaseResults'][this.editModel]
      }`)?.default
      return componet
    },
    // 基础信息表单配置项
    formList() {
      return [
        {
          title: '基础信息'
        },
        {
          keyName: 'yearDate',
          type: 'BaseInput',
          disabled: true,
          label: '年度时间'
        },
        {
          keyName: 'reviewStage',
          type: 'BaseInput',
          disabled: true,
          label: '评审阶段',
          attrs: {
            addonBefore: '第',
            addonAfter: '次'
          }
        },
        {
          keyName: 'launchEmployeeName',
          type: 'BaseInput',
          disabled: true,
          label: '发起人'
        },
        {
          keyName: 'launchTime',
          type: 'BaseInput',
          disabled: true,
          label: '发起时间'
        },
        {
          keyName: 'reviewEndTime',
          type: 'a-date-picker',
          disabled: this.editModel != 1,
          hidden: this.editModel == 0 || this.editModel == 3,
          attrs: {
            valueFormat: 'YYYY-MM-DD'
          },
          label: '评审结束时间'
        }
      ]
    }
  },
  data() {
    return {
      api: qcActivityAchievementReview, // 请求api
      titles: ['查看QC活动成果资料评审信息', '发起资料评审', '资料扣分', '发布成果'],
      loading: false, // 加载控件
      editModel: 0, // 操作类型
      form: {
        yearDate: void 0,
        reviewStage: void 0,
        launchEmployeeId: void 0,
        launchEmployeeName: void 0,
        launchTime: void 0,
        reviewEndTime: void 0
      },
      rules: {
        reviewEndTime: [{ required: true, message: '请选择评审结束时间', trigger: 'change' }]
      } // 表单校验规则
    }
  },
  mounted() {
    // 非发起评审时请求详情
    if (this.editModel != 1) {
      this.loading = true
      let args = { id: this.id }
      this.editModel == 3 && (args.isComplete = true)
      this.api
        .getDetail(args)
        .then((res) => {
          this.loading = false
          if (res.code === 200) {
            this.form = res.data
          }
        })
        .catch(() => {
          this.loading = false
        })
    } else {
      // 初始化基础信息
      this.form.yearDate = this.moment(new Date()).format('YYYY')
      this.form.launchEmployeeName = this.$userInfo()?.name
      this.form.launchEmployeeId = this.$userInfo()?.employee.id
      this.form.launchTime = this.moment(new Date()).format('YYYY-MM-DD')
      // 获取评审阶段
      qcActivityAchievementReview.getStage(this.$currentPost().id).then((res) => {
        if (res.code === 200) {
          this.form.reviewStage = res.data
        }
      })
    }
  },
  methods: {
    /**
     * 提交表单
     */
    handleSubmit() {
      this.$refs.ruleForm.validate((res) => {
        if (res) {
          res && this.$refs.table.handleSubmit()
        } else {
          this.$message.warning('评审结束时间不能为空！')
        }
      })
    }
  }
}
</script>
